// api
import cartModelApi from '@/request/api/cart.js'

const cartKey = 'cart_key';

export default{
    state:{
      // 保存当前用户购物车列表
      cartList: [],
      selectList: [],
      // 用户购物车订单数量（order_status != 2）
      countTip: 0
    },
    getters:{
        
    },
    mutations:{
      setCartList(state, data){
        state.cartList = data;
        state.countTip = data.filter(item => item['order_status'] != 2).length;
        localStorage.setItem(cartKey, JSON.stringify(data));
      },
      setSelList(state, data){
        state.selectList = data;
      }
    },
    actions:{
       async getCartList({ commit }, username){
         return new Promise(async (resolve) => {
            cartModelApi.getCartListApi({
              user_name: username
            }).then(res => {
              // 请求获取购物车接口并存入
              commit('setCartList', res.data);
            })
            resolve();
         })
            // cartModelApi.getCartListApi({
            //   user_name: username
            // }).then(res => {
            //   // 请求获取购物车接口并存入
            //   commit('setCartList', res.data);
            // })
       }
    },
}  